using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using AUSRIS.Reports.Extensions.Security.ReportService2005;

namespace AUSRIS.Reports.Extenstions.Security.Utility
{
    class Program
    {
        static void Main(string[] args)
        {
            WMIQueryReportServer("maxap04602");
            System.Console.ReadLine();
            QueryReportServer("integration");
        }

        private static void QueryReportServer(string p)
        {
            try
            {
                // ReportingService2005 rs = new ReportingService2005();
                
            }
            catch(Exception ex)
            {
            }
            finally
            {
            }
        }

        static void WMIQueryReportServer(string machineName)
        {
            
            string wmiNamespace = @"\\" + machineName + @"\root\Microsoft\SqlServer\ReportServer\v9\Admin";
            // root\Microsoft\SqlServer\ReportServer\v9\Admin
            // MSReportManager_ConfigurationSetting
            // MSReportServer_ConfigurationSetting
            ManagementScope managementScope = null;
            ConnectionOptions connectionOptions = new ConnectionOptions();
            connectionOptions.Username = @"MAXWELL\jamie.swindall.adm";
            connectionOptions.Password = @"Bir100Jb$Bir100Jb$";
            connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;

            try
            {
                managementScope = new ManagementScope(wmiNamespace, connectionOptions);
                managementScope.Connect();
                // ManagementPath managementPath = new ManagementPath("MSReportManager_ConfigurationSetting");
                List<ManagementPath> managementPaths = new List<ManagementPath>();
                managementPaths.Add(new ManagementPath("MSReportManager_ConfigurationSetting"));
                managementPaths.Add(new ManagementPath("MSReportServer_ConfigurationSetting"));
                foreach(ManagementPath managementPath in managementPaths)
                {
                    Console.WriteLine("********************************");
                    Console.WriteLine(String.Format("{0}", managementPath.ClassName));
                    Console.WriteLine("********************************");

                    ManagementClass managementClass = new ManagementClass(managementScope, managementPath, new ObjectGetOptions());
                    managementClass.Get();                    
                    if(managementClass == null)
                    {
                        // throw new Exception("No WMI class found.");
                        Console.WriteLine("No WMI class found.");
                    }
                    else
                    {
                        foreach(ManagementObject managementObject in managementClass.GetInstances())
                        {
                            managementObject.Get();
                            Console.WriteLine("---------------------------------");
                            Console.WriteLine(String.Format("{0}", managementObject.Path.RelativePath));
                            Console.WriteLine("---------------------------------");
                            foreach(PropertyData propertyData in managementObject.Properties)
                            {
                                Console.WriteLine(String.Format("{0}: {1}", propertyData.Name, propertyData.Value.ToString()));
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(String.Format("##### {0} #####",DateTime.Now.ToFileTime().ToString()));
                Console.WriteLine(String.Format("Exception.Message: {0}",ex.Message));
                Console.WriteLine(String.Format("Exception.InnerException: {0}", ex.InnerException));
                Console.WriteLine(String.Format("Exception.StackTrace: {0}", ex.StackTrace));
                Console.WriteLine(String.Format("Exception.Source: {0}", ex.Source));
                Console.WriteLine("..........................");
                // throw new Exception("An error occurred while attempting to get the ReportServer Url.", ex);
                return;
            }

            
        }
    }
}
